package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.zhgd.util.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.dto.AsphaltRequiresDto;
import com.ruoyi.zhgd.service.IZjNWarningUsingService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryAsphaltRequiresRefBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryAsphaltRequiresRefVo;
import com.ruoyi.zhgd.domain.NLaboratoryAsphaltRequiresRef;
import com.ruoyi.zhgd.mapper.NLaboratoryAsphaltRequiresRefMapper;
import com.ruoyi.zhgd.service.INLaboratoryAsphaltRequiresRefService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 沥青技术要求_列表Service业务层处理
 *
 * @author cj
 * @date 2023-09-21
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryAsphaltRequiresRefServiceImpl implements INLaboratoryAsphaltRequiresRefService {

    private final NLaboratoryAsphaltRequiresRefMapper baseMapper;

    private final IZjNWarningUsingService iZjNWarningUsingService;

    /**
     * 查询沥青技术要求_列表
     */
    @Override
    public NLaboratoryAsphaltRequiresRefVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询沥青技术要求_列表列表
     */
    @Override
    public TableDataInfo<NLaboratoryAsphaltRequiresRefVo> queryPageList(NLaboratoryAsphaltRequiresRefBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryAsphaltRequiresRef> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryAsphaltRequiresRefVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青技术要求_列表列表
     */
    @Override
    public List<NLaboratoryAsphaltRequiresRefVo> queryList(NLaboratoryAsphaltRequiresRefBo bo) {
        LambdaQueryWrapper<NLaboratoryAsphaltRequiresRef> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryAsphaltRequiresRef> buildQueryWrapper(NLaboratoryAsphaltRequiresRefBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryAsphaltRequiresRef> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAsphaltId()), NLaboratoryAsphaltRequiresRef::getAsphaltId, bo.getAsphaltId());
        lqw.eq(bo.getMax() != null, NLaboratoryAsphaltRequiresRef::getMax, bo.getMax());
        lqw.eq(bo.getMin() != null, NLaboratoryAsphaltRequiresRef::getMin, bo.getMin());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), NLaboratoryAsphaltRequiresRef::getType, bo.getType());
        lqw.eq(bo.getTemperature() != null, NLaboratoryAsphaltRequiresRef::getTemperature, bo.getTemperature());
        lqw.eq(StringUtils.isNotBlank(bo.getLevel()), NLaboratoryAsphaltRequiresRef::getLevel, bo.getLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleted()), NLaboratoryAsphaltRequiresRef::getDeleted, bo.getDeleted());
        return lqw;
    }

    /**
     * 新增沥青技术要求_列表
     */
    @Override
    public Boolean insertByBo(NLaboratoryAsphaltRequiresRefBo bo) {
        NLaboratoryAsphaltRequiresRef add = BeanUtil.toBean(bo, NLaboratoryAsphaltRequiresRef.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青技术要求_列表
     */
    @Override
    public Boolean updateByBo(NLaboratoryAsphaltRequiresRefBo bo) {
        NLaboratoryAsphaltRequiresRef update = BeanUtil.toBean(bo, NLaboratoryAsphaltRequiresRef.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryAsphaltRequiresRef entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青技术要求_列表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public PageUtils queryPage(Page page, Map<String, Object> params) {
        Long deptId = LoginHelper.getDeptId();
        List<SysDept> collect = iZjNWarningUsingService.getDeptTreeTop(deptId);
        List<Long> deptIds = collect.stream().map(SysDept::getDeptId).collect(Collectors.toList());
        params.put("deptIds", deptIds);
        List<AsphaltRequiresDto> list = baseMapper.queryPages(page, params);
        return new PageUtils(page.setRecords(list));
    }

    @Override
    public AsphaltRequiresDto detail(String id) {
        return baseMapper.detail(id);
    }

    @Override
    public List<NLaboratoryAsphaltRequiresRef> selectRequiresRef(String testType, String type) {
        List<NLaboratoryAsphaltRequiresRef> requiresRef = baseMapper.selectRequiresRef(testType,type);
        return requiresRef;
    }

    @Override
    public List<NLaboratoryAsphaltRequiresRef> getRequiresRefYd(String testType, String type) {
        List<NLaboratoryAsphaltRequiresRef> requiresRef = baseMapper.getRequiresRefYd(testType,type);
        return requiresRef;
    }
}
